package com.xmb.wechat.delegate;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xmb.wechat.R;
import com.xmb.wechat.baserecyclerview.BaseRecyclerViewManager;
import com.xmb.wechat.definterface.OnPYQCommentsDelegateListener;
import com.xmb.wechat.bean.PYQCommentsBean;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/12 09:24
 */
public class PYQAddCommentsDelegate extends BaseRecyclerViewManager {

    private boolean                       isHasEdit;
    private OnPYQCommentsDelegateListener listener;

    public PYQAddCommentsDelegate(Activity activity, OnPYQCommentsDelegateListener listener) {
        super(activity);
        this.listener = listener;
    }

    @Override
    public BaseQuickAdapter initAdapter() {
        return new BaseQuickAdapter<PYQCommentsBean, BaseViewHolder>(R.layout.item_pyq_add_comments, new ArrayList<>()) {
            @Override
            protected void convert(BaseViewHolder helper, PYQCommentsBean item) {
                if (item.getType() == 0) {
                    helper.setText(R.id.tv_title, TextUtils.isEmpty(item.getCommenter()) ? "" : item.getCommenter() + "（评论）");
                } else {
                    helper.setText(R.id.tv_title, TextUtils.isEmpty(item.getCommenter()) ? "" : item.getCommenter() + "（回复）" + item.getCommenteder());
                }
                helper.setText(R.id.tv_content, TextUtils.isEmpty(item.getCommentValue()) ? "" : item.getCommentValue());
                helper.getView(R.id.iv_delete).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeItem(mData.indexOf(item));
                        isHasEdit = true;
                    }
                });
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null)
                            listener.onItemClick(item);
                    }
                });
            }
        };
    }

    @Override
    protected RecyclerView.ItemDecoration getItenDecoration() {
        return new HorizontalDividerItemDecoration.Builder(context).size(SizeUtils.dp2px(0.5f)).color(ContextCompat.getColor(context, R.color.black_10)).build();
    }

    public boolean hasDelete() {
        return isHasEdit;
    }
}
